package fileProcessors.implementations;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import fileProcessors.FileFormatException;
import fileProcessors.FileProcessor;

public class FrapConverterSimulation extends FileProcessor {

	public FrapConverterSimulation() {
		super("FrapConverterSimulation", ".dat", "_converted.txt");
	}

	@Override
	public void processFiles(File files[], File currentDirectory, File outputFile) throws FileFormatException {
		try {
			BufferedWriter bwResult = new BufferedWriter(new FileWriter(outputFile));
			for (int i = 0; i < files.length; i++) {
				bwResult.write("Loading " + files[i].getName() + ".");
				bwResult.newLine();

				BufferedReader br = new BufferedReader(new FileReader(files[i]));
				String line = br.readLine();

				for (String nextFileNameIndicator = findNextFileNameIndicator(br); nextFileNameIndicator != null; nextFileNameIndicator = findNextFileNameIndicator(br)) {
					String filename=extractFileName(nextFileNameIndicator);
					BufferedWriter bw=new BufferedWriter(new FileWriter(new File(filename)));
					//jetzt einlesen der Daten
					for(line=br.readLine(); !line.contains("#frap curve"); line=br.readLine()){
						line=line.trim();
						line=line.replace(" ", "\t");
						bw.write(line);
						bw.newLine();
						if(line==null){
							break;
						}
					}
					bw.close();
				}
			}
			bwResult.close();
		} catch (IOException e1) {
			throw new FileFormatException("Fehler beim schreiben in Ergebnisdatei.");
		}
	}

	private String extractFileName(String nextFileNameIndicator) {
		if(nextFileNameIndicator==null){
			return null;
		}
		try{
			ArrayList<String> zahlen=regexChecker("\\d*\\.\\d+", nextFileNameIndicator);
			return "N"+zahlen.get(0)+"D"+zahlen.get(3)+"kon"+zahlen.get(4)+"koff"+zahlen.get(5)+"bleach"+zahlen.get(7)+".txt";
		}catch(RuntimeException e){
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * @param br
	 * @return null wenn keine Zeile vorhanden ist
	 * @throws IOException
	 */
	private String findNextFileNameIndicator(BufferedReader br) throws IOException {
		br.mark(100000);
		String line = br.readLine();
		while (!line.trim().contains("#N dx dt D kon koff Golgi-fraction, bleach, tmax")) {
			line = br.readLine();
			if (line == null) {
				return null;
			}
		}
		// dann n�chste Zeile ausgeben
		String nextFileNameIndicator = br.readLine();
		br.reset();
		return nextFileNameIndicator;
	}

	@Override
	public String getInformation() {
		return ("List alle Eingabedateien ein und sucht nach dem Ende der Bleach-Phase. Alle Zeilen "
				+ "ab dem Ende der BleachPhase werden anschlie�end in eine Ausgabedatei gleichen Namens geschrieben.");
	}

	@Override
	public String getDate() {
		return ("20.06.2013");
	}

	public static ArrayList<String> regexChecker(String theRegex, String str2Check) {
		Pattern checkRegex = Pattern.compile(theRegex);
		Matcher regexmatcher = checkRegex.matcher(str2Check);
		ArrayList<String> matches = new ArrayList<String>();
		while (regexmatcher.find()) {
			if (regexmatcher.group().length() != 0) {
				matches.add(regexmatcher.group().trim());
			}
		}
		return matches;
	}

}
